package com.yonyou.base.flower.web;

import com.yonyou.base.common.utils.UUIDUtils;
import com.yonyou.base.common.web.MessageResult;
import com.yonyou.base.flower.model.Flower;
import com.yonyou.base.flower.service.IFlowerStorageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 存储模块
 */
@Controller
@RequestMapping("/flower/storage")
public class FlowerStorageController {

    @Resource
    private IFlowerStorageService flowerStorageService;

    /**
     * 保存图鉴
     *
     * @param flower 植物图鉴
     * @return Object
     */
    @RequestMapping("/saveFlower.do")
    @ResponseBody
    public Object saveFlower(@RequestBody Flower flower) {
        MessageResult result = new MessageResult(MessageResult.STATUS_SUCCESS, "操作成功");
        if (StringUtils.isBlank(flower.getId())) {
            flower.setId(UUIDUtils.getRandomUUID());
            flower.setCreationtime(System.currentTimeMillis());
            flowerStorageService.insertFlower(flower);
        } else {
            flower.setModifier(flower.getCreator());
            flower.setModifiedtime(System.currentTimeMillis());
            flowerStorageService.updateFlower(flower);
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("flower", flower);
        result.setData(params);
        return result;
    }

    /**
     * 删除图鉴
     *
     * @param flowerId 图鉴主键
     * @return Object
     */
    @RequestMapping("/deleteFlower.do")
    @ResponseBody
    public Object deleteFlower(String flowerId) {
        flowerStorageService.deleteFlower(flowerId);
        return MessageResult.SUCCESS;
    }

    /**
     * 查询图鉴
     *
     * @param userId 用户id
     * @param page   页码
     * @param size   条数
     * @return Object
     */
    @RequestMapping("/queryFlower.do")
    @ResponseBody
    public Object queryFlower(String userId, Integer page, Integer size) {
        MessageResult result = new MessageResult(MessageResult.STATUS_SUCCESS, "操作成功");
        Page<Flower> flowers = flowerStorageService.queryFlower(userId, page, size);
        HashMap<String, Object> params = new HashMap<>();
        params.put("flowers", flowers);
        result.setData(params);
        return result;
    }

    @RequestMapping("/homeFlower.do")
    @ResponseBody
    public Object homeFlower() {
        MessageResult result = new MessageResult(MessageResult.STATUS_SUCCESS, "操作成功");
        Flower flower = flowerStorageService.queryHomeFlower();
        HashMap<String, Object> params = new HashMap<>();
        params.put("flower", flower);
        result.setData(params);
        return result;
    }
}
